<% if rfc_cis.present? || rfc_relations.present? %>
  <% rfc_relations_map = rfc_relations.inject({}) do |m, r|
      key = "#{r.ci_hash['toCiName']}***#{r.ci_hash['toCiClass']}"
       m[key] ||= []
       m[key] << r
      key = "#{r.ci_hash['fromCiName']}***#{r.ci_hash['fromCiClass']}"
       m[key] ||= []
       m[key] << r
       m
     end %>
  <div class="rfc-list accordion" id="<%= random_dom_id %>" data-log-url="<%= log_data_assembly_transition_environment_deployment_path(@assembly, @environment, @deployment) if @deployment && @deployment.persisted? %>">
    <% rfc_cis.group_by(&:execOrder).sort.each do |exec_order, step_rfcs| %>
      <div class="step">
        <% step_duration = @time_stats && @time_stats[:steps] && @time_stats[:steps][exec_order] %>
        <div class="name" onclick='$j(this).find("i").toggleClass("fa-caret-up fa-caret-down").parents(".step").find(".step-content").toggle(500)'>
          <strong>Step <%= exec_order %></strong>
          <% if step_duration %>
            <small>( <%= time_duration_in_words(step_duration) %> )</small>
          <% end %>
          <%= icon('caret-up') %>
        </div>
        <div class="step-content">
          <% step_rfcs.group_by {|rfc| rfc.nsPath}.sort.each do |ns_path, ns_rfcs| %>
            <% platform_name = ns_path.split('/bom/').last %>
            <% ns_rfcs.group_by {|rfc| rfc.ciClassName}.sort.each do |component_class, class_rfcs| %>
              <% component_type = component_class.split('.').last %>
              <% class_rfcs.group_by {|rfc| rfc.ciName.split('-')[0..-3].join('-')}.sort.each do |component, component_rfcs| %>
                <% component_rfcs.group_by { |rfc| rfc.ciName.split('-')[-2].to_i }.sort.each do |cloud_id, cloud_rfcs| %>
                  <ul class="inline component rounded shadow white" onclick="if (event.target.tagName.toUpperCase() != 'A') $j(this).next().toggle(300)">
                    <li>
                      <% platform = @platforms[platform_name] %>
                      <% if platform %>
                        <%= image_tag(platform_image_url(platform), :alt => platform_name) %>
                        <%= platform.ciAttributes.pack %>
                        <b><%= platform.ciName %> v<%= platform.ciAttributes.major_version %></b>
                      <% else %>
                        <b><%= platform_name %></b>
                      <% end %>
                    </li>
                    <li>
                      <%= image_tag ci_class_image_url(component_class), :alt => component_type %>
                      <b><%= link_to(component, edit_assembly_transition_environment_platform_component_path(@assembly, @environment, platform, component)) if platform %></b>
                    </li>
                    <% cloud = @clouds[cloud_id] %>
                    <li class="pull-right">
                      <%= icon('cloud', link_to(cloud ? link_to(cloud.ciName, edit_cloud_path(cloud)) : cloud_id)) %>
                      <% cloud_deployment_state_summary = class_rfcs.group_by{|rfc_ci| rfc_ci.deployment.dpmtRecordState}
                         cloud_deployment_state = ''
                         %w(failed canceled pending complete).each do |s|
                           if cloud_deployment_state_summary[s]
                             cloud_deployment_state = s
                             break
                           end
                         end %>
                      &nbsp;
                      <%= rfc_state_icon(cloud_deployment_state) %></li>
                  </ul>
                  <ul class="rfc">
                    <% cloud_rfcs.each do |rfc_ci| %>
                      <% rfc_id = rfc_ci.rfcId %>
                      <li class="rfc" data-bom-rfc-id="<%= rfc_id %>">
                        <div class="accordion-group">
                          <div class="accordion-heading">
                            <div class="accordion-toggle">
                              <% rfc_time_stats = @time_stats.present? && @time_stats[:rfcs][rfc_id]
                                 total_time = rfc_time_stats && rfc_time_stats['totalTime']
                                 deployment_record_state = rfc_ci.deployment.dpmtRecordState %>
                              <span>
                                <%= rfc_action_icon(rfc_ci.rfcAction, 'fa-lg') %>&nbsp;&nbsp;
                                <strong>
                                  <% if (deployment_record_state != 'complete' && rfc_ci.rfcAction == 'add') || (deployment_record_state == 'complete' && rfc_ci.rfcAction == 'delete') %>
                                    <%= rfc_ci.ciName %>
                                  <% else %>
                                    <%= link_to(rfc_ci.ciName, path_to_ci(rfc_ci)) %>
                                  <% end %>
                                </strong>
                              </span>
                              <ul class="nav inline pull-right">
                                <li><%= link_to_function('Changes', "toggle_rfc_tab(this, '#{rfc_id}', 'changes')") %></li>
                                <% unless deployment_record_state == 'pending' %>
                                  <li class="log-tab-trigger"><%= link_to_function('Log', "toggle_rfc_tab(this, '#{rfc_id}', 'log')") %></li>
                                <% end %>
                                <li></li>
                                <li>
                                  <% if total_time %>
                                    <small class="rfc-duration"><%= link_to_function(time_duration_in_words(total_time.to_f * 1000), "toggle_rfc_tab(this, '#{rfc_id}', 'stats')") %></small>
                                  <% end %>
                                  <%= rfc_state_icon(deployment_record_state, 'fa-lg') %>
                                </li>
                              </ul>
                            </div>
                          </div>
                          <div class="accordion-inner" data-rfc-id="<%= rfc_id %>" style="display:none">
                            <div class="tab-pane rfc-changes">
                              <% rfc_ci_relations = rfc_relations_map["#{rfc_ci.ciName}***#{rfc_ci.ciClassName}"] %>
                              <% if rfc_ci_relations %>
                                <ul class="nav nav-tabs">
                                  <li class="active"><a href="#rfc_<%= rfc_id %>_tab_changes_attributes" data-toggle="tab">Attributes</a></li>
                                  <li><a href="#rfc_<%= rfc_id %>_tab_changes_relations" data-toggle="tab">Relations</a></li>
                                </ul>
                                <div class="tab-content">
                                  <div class="tab-pane active" id="rfc_<%= rfc_id %>_tab_changes_attributes">
                                    <%= rfc_properties(rfc_ci) %>
                                    <%= rfc_attributes(rfc_ci) %>
                                  </div>
                                  <div class="tab-pane" id="rfc_<%= rfc_id %>_tab_changes_relations">
                                    <% rfc_ci_relations.each do |r| %>
                                      <div class="category">
                                        <div class="name"><%= r.relationName.split('.').last %></div>
                                        <%= rfc_properties(r) %>
                                        <%= rfc_attributes(r) %>
                                      </div>
                                    <% end %>
                                  </div>
                                </div>
                              <% else %>
                                <%= rfc_properties(rfc_ci) %>
                                <%= rfc_attributes(rfc_ci) %>
                              <% end %>
                            </div>
                            <div class="tab-pane rfc-log"></div>
                            <div class="tab-pane rfc-stats"><%= hash_list(rfc_time_stats) if rfc_time_stats %></div>
                          </div>
                        </div>
                      </li>
                    <% end %>
                  </ul>
                <% end %>
              <% end %>
            <% end %>
          <% end %>
        </div>
      </div>
    <% end %>
  </div>
  <script type="text/javascript">
    <%= render 'cms/rfcs/rfc' %>
  </script>
<% end %>
